/*
 *Author:Jackong
 *Date:2012/07/04
 */
#include "StdAfx.h"
#include "recv_buf.h"

namespace fly {

	namespace jld{

		namespace buffer {	

			recv_buf::recv_buf()
				:base_buf()
				,body_size_(0)
			{
			}

			recv_buf::~recv_buf()
			{
			}

			//check recv_buf=header size whether right and parse it
			bool recv_buf::handle_header()
			{
				LOG_INFO("Handle header");
				if (header_size_ != buf_.size())
				{
					LOG_EMER("Header size is not ", integer(header_size_));
					return false;
				}
				Header header;
				if (!parse_from_recv(header))
				{
					LOG_EMER("Parse header faild");
					return false;
				}
				if (min_buf_size_ > header.size())
				{
					LOG_EMER("Body size must be greater than or equals to", integer(min_buf_size_));
					return false;
				}
				body_size_ = header.size();
				pid_ = header.pid();
				return true;
			}

			const size_t& recv_buf::body_size()
			{
				return body_size_;
			}

			
			bool recv_buf::parse_from_recv(google::protobuf::Message& message)
			{
				if (min_buf_size_ > buf_.size())
				{
					LOG_ERR("Recv buffer size must be greater than or equals to ", integer(min_buf_size_));
					return false;
				}
				istream istr(&buf_);
				bool is_parse = message.ParseFromIstream(&istr);
				istr.clear();
				return is_parse;
			}

			PID& recv_buf::pid()
			{
				return pid_;
			}
		}
	}
}